<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
       <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换时间" prop="createDate">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
         <el-form-item label="兑换价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入兑换价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['exchange:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

      <div>
        <span>总次数：{{ totalForm.infoTotal }}</span></br>
        <span>总获得积分/SHJC：{{ totalForm.balanceTotal}}</span></br>
        <span>总数量/张：{{ totalForm.cardTotal }}</span>
      </div>

    <el-table v-loading="loading" :data="recordList">
      <el-table-column label="用户id" align="center" prop="userId" sortable="custom" @sort-change="handleSortChange"/>
      <el-table-column label="用户名" align="center" prop="userName" sortable="custom" @sort-change="handleSortChange"/>

      <el-table-column label="兑换时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量/张" align="center" prop="cardCount" sortable="custom" @sort-change="handleSortChange"/>
      <el-table-column label="兑换价格" align="center" prop="price">
        <template slot-scope="scope">
          <span>￥{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总金额" align="center">
        <template slot-scope="scope">
          <span>￥{{ scope.row.exchangeAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exchange:record:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog title="兑换详情" :visible.sync="open" width="500px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="用户id">{{ form.userId }}</el-descriptions-item>
        <el-descriptions-item label="兑换时间">{{ parseTime(form.exchangeTime) }}</el-descriptions-item>
        <el-descriptions-item label="数量/张">{{ form.cardCount }}</el-descriptions-item>
        <el-descriptions-item label="兑换价格">￥{{ form.price }}</el-descriptions-item>
        <el-descriptions-item label="总金额">￥{{ form.exchangeAmount }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExchangeRecord,totalExchangeList,delExchangeRecord} from "@/api/exchange/record";

export default {
  name: "ExchangeRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 兑换记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        exchangeTime: undefined,
      },
      // 表单参数
      form: {},
      totalForm: {
        infoTotal: 0,
        balanceTotal:0,
        cardTotal:0
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询兑换记录列表 */
  async  getList() {
      this.loading = true;
      // 处理日期范围和排序
      let params = {
        ...this.queryParams,
      };
      if (this.dateRange && this.dateRange.length === 2) {
        params.startTime = this.dateRange[0];
        params.endTime = this.dateRange[1];
      }
      listExchangeRecord(params).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      /** 统计数据 */
      const res = await totalExchangeList(params);
      if (res.code == 200) { 
        this.totalForm = res.data;
      }
    },
       /** 删除按钮操作 */
    handleDelete(row) {
      console.log("row",row)
      const exchangeIds = row.exchangeId || this.ids;
      this.$modal
        .confirm('是否确认删除兑换记录编号为"' + exchangeIds + '"的数据项？')
        .then(function () {
          return delExchangeRecord(exchangeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.form = row;
      this.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.recordList.length > 0) {
        let params = {
          ...this.queryParams
        };
        if (this.dateRange && this.dateRange.length === 2) {
          params.startTime = this.dateRange[0];
          params.endTime = this.dateRange[1];
        }
        this.download('system/exchange/export', params, `兑换记录表${new Date().getTime()}.xlsx`)
      } else { 
        this.$message.warning("未查询到数据");
      }
   },
    /** 排序触发事件 */
    handleSortChange(column) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order === 'ascending' ? 'asc' : 'desc';
      this.getList();
    }
  }
};
</script> 